package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import school.domain.Message;
import school.mapper.MessageMapper;
import school.support.BaseController;
import school.support.pojo.MessagePo;
import school.support.util.ParamUtils;

import java.util.Date;

@RequestMapping("message")
@Controller
public class MessageController extends BaseController {

    @Autowired
    private MessageMapper messageMapper;

    @RequestMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<MessagePo> pageInfo = new PageInfo<MessagePo>(messageMapper.selectByUser(sessionUser().getId()), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "message/list";
    }

    /**
     * 发送信息
     *
     * @param message
     * @return
     */
    @PostMapping("add")
    public String add(Message message) {
        message.setStatus("no");
        message.setSendid(sessionUser().getId());
        message.setAddtime(new Date());
        messageMapper.insertSelective(message);
        return refresh();
    }


}
